** Generates a file with hospital patient counts for in sample hospitals

** Set up
clear
cd "/disk/agedisk3/medicare.work/chandra-DUA52080/pragya-dua52080/replication/"
log using CCP_mktsh.log, replace

** List of race groups
local groups "b w"

** List of reweight share types (weighted zip code, weighted HRR)
local sharetypes_reweight "wgt wgtH"

** PART 1: Creates file with hospital IDs 
** + black and white patient counts (censoring patient counts for hospitals with > 0 & < 11 patients) 
** + white patient counts reweighted at the zip and HRR level for hospitals (censoring for hospitals with > 0 or < 11 white patients)

	clear
	use pn longsurv_ami_fere_a yidx npatients_b_ami_tot_act npatients_w_ami_tot_act npatients_w_ami_tot_wgtH npatients_w_ami_tot_wgt  if ((yidx == 1 | yidx == 4) & longsurv_ami_fere_a != . )  using  Intermediate_Output_Not_Exportable/bs0.dta, replace
	keep pn yidx npatients*
	
	** limit to balanced sample
	bysort pn: gen obs_num = _N
	drop if obs_num == 1 

	foreach heto in `groups'{
		replace npatients_`heto'_ami_tot_act = . if npatients_`heto'_ami_tot_act > 0 & npatients_`heto'_ami_tot_act < 11
		count if npatients_`heto'_ami_tot_act == .
		assert r(N) != . 
	}	
	
	replace npatients_w_ami_tot_wgtH = . if npatients_w_ami_tot_act == .
	replace npatients_w_ami_tot_wgt = . if npatients_w_ami_tot_act == .

	reshape wide npatients_b_ami_tot_act npatients_w_ami_tot_act npatients_w_ami_tot_wgt npatients_w_ami_tot_wgtH, i(pn) j(yidx)
	rename pn pn_new
	capture mkdir Exportable_IntermediateData/
	save  Exportable_IntermediateData/hospitals_black_white_patcount.dta, replace	
	
** PART 2: Generates a file with some total numbers
		clear
		use pn longsurv_ami_fere_a npatients* yidx  if ((yidx == 1 | yidx == 4) & longsurv_ami_fere_a != . )  using  Intermediate_Output_Not_Exportable/bs0.dta, replace

		** limit to balanced sample
		bysort pn: gen obs_num = _N
		drop if obs_num == 1 

		** Number of patients at hospitals with 1-10 white or 1-10 black patients
		generate npatients_b_ami_tot_act_cen = npatients_b_ami_tot_act if npatients_b_ami_tot_act > 0 & npatients_b_ami_tot_act < 11
		generate npatients_w_ami_tot_act_cen = npatients_w_ami_tot_act if npatients_w_ami_tot_act > 0 & npatients_w_ami_tot_act < 11

		** Number of white patients reweighted according to black patient distribution at zip or HRR level 
		** among hospitals with 1-10 white patients
		generate npatients_w_ami_tot_wgt_cen = npatients_w_ami_tot_wgt if npatients_w_ami_tot_act > 0 & npatients_w_ami_tot_act < 11
		generate npatients_w_ami_tot_wgtH_cen = npatients_w_ami_tot_wgtH if npatients_w_ami_tot_act > 0 & npatients_w_ami_tot_act < 11

		** Generate hospital count
		isid pn yidx

		collapse (sum) npatients_b_ami_tot_act npatients_w_ami_tot_act npatients_w_ami_tot_wgt npatients_w_ami_tot_wgtH npatients_b_ami_tot_act_cen npatients_w_ami_tot_act_cen npatients_w_ami_tot_wgt_cen npatients_w_ami_tot_wgtH_cen, by(yidx) 
		
		** various checks
		assert npatients_b_ami_tot_act - npatients_b_ami_tot_act_cen > 10 | npatients_b_ami_tot_act - npatients_b_ami_tot_act_cen == 0 
		assert npatients_w_ami_tot_act - npatients_w_ami_tot_act_cen > 10 | npatients_w_ami_tot_act - npatients_w_ami_tot_act_cen == 0 		

		foreach var in npatients_b_ami_tot_act npatients_w_ami_tot_act npatients_w_ami_tot_wgt npatients_w_ami_tot_wgtH npatients_b_ami_tot_act_cen npatients_w_ami_tot_act_cen npatients_w_ami_tot_wgt_cen npatients_w_ami_tot_wgtH_cen{
		assert `var' == 0 | `var' > 10
		rename `var' s`var'
		}
		
		
		gen byte id = 1
		reshape wide snpatients* , i(id) j(yidx)

	save  Exportable_IntermediateData/hospital_totalcounts.dta, replace	
	







